game = {}
debug = false

function game:init()
  --Load the player. Might do in the beginning with everythin else depending on load time later in the project.
  require('player')
  require('enemy')
  require('ai')
  --Handles animation for an explosion
  anim8 = require('anim8')
  explosion = {}
  --Image for explosion
  explosion.i = love.graphics.newImage('game/explosion.png')
  --explosion.s = 
  --Create the camera. This handles tracking the player for us.
  camera = Camera(0,0)
  
  --The crosshairs that follow the mouse
  crosshairs = love.graphics.newImage('game/crosshairs.png')
  
  gameMusic = love.audio.newSource('game/Sonar2.ogg')
  gameMusic:setLooping(true)
  gameMusic:setVolume(0.6)
  
  hud = {}
  --Stop player from shooting a ping or missile if their mouse is on the HUD
  hud.focus = false
  hud.health = love.graphics.newImage('game/HUD_HealthTitle.png')
  hud.quit = {i = love.graphics.newImage('game/HUD_Quit.png'), on = false}
  hud.enemy = love.graphics.newImage('game/HUD_Enemy.png')
  hud.audio = {i = love.graphics.newImage('game/HUD_Audio.png'), on = true}
  hud.reload = love.graphics.newImage('game/HUD_Reloading.png')
  hud.victory = love.graphics.newImage('game/victory.png')
  hud.defeat = love.graphics.newImage('game/defeat.png')
  
end

function game:enter()
  love.audio.play(gameMusic)
  --Get screen size
  screen.size = {w = love.graphics.getWidth(), h = love.graphics.getHeight()}
  --Hide the mouse on starting the game
  love.mouse.setVisible(false)
  if debug then
    camera:zoomTo(0.3)
    camera:lookAt((1920*3*screen.scale)/32*16,(1920*3*screen.scale)/32*9)
  end
    
  
  --Create a grid size to scale obstacles by.
  grid = (1920*3*screen.scale)/32
  --Clear any stored map
  map = nil
  map = {}
  --Create a new map and get the player's grid position and do the same for the enemy.
  map,player.pos.gx,player.pos.gy,enemy.pos.gx,enemy.pos.gy = createMap(32,18,8, false)
  
  --Create physics world
  love.physics.setMeter(grid/10)
  world = love.physics.newWorld(0, 0, true)
  world:setCallbacks(beginContact)
  
  bodies = {}
  for i = 1, #map, 1 do
   for j = 1, #map[1], 1 do
      if map[i][j] == 1 then
        local b = #bodies + 1
        bodies[b] = {}
        bodies[b].body = love.physics.newBody(world, i*grid, j*grid)
        bodies[b].shape = love.physics.newRectangleShape(grid, grid)
        bodies[b].fixture = love.physics.newFixture(bodies[b].body, bodies[b].shape)
        bodies[b].fixture:setCategory(4)
      end
    end
  end
  player:create()
  enemy:create()
  
  --init AI
  ai:init(map, enemy, player)
end
function game:leave()
  love.audio.stop(gameMusic)
end

--
--
--Called when there is a collision between any two objects
function beginContact(a, b, coll)
    local catA = a:getCategory()
    local catB = b:getCategory()
    --Did a missle hit something?
    if catA == 6 or catB == 6 or catA == 7 or catB == 7 then
      local s = love.audio.newSource(player.expMissile, 'static')
      love.audio.play(s)
    end
    
    --Did a missile hit the enemy?
    if (catA == 3 and catB == 6) or (catA == 6 and catB == 3) then
      enemy.health = enemy.health - 1
      print("Enemy health at "..enemy.health)
    end
    --Did a missile hit the player?
    if (catA == 1 and catB == 7) or (catA == 7 and catB == 1) then
      player.health = player.health - 1
      print("Player health at "..player.health)
    end
    
    
end

--

function game:update(dt)
  world:update(dt)
  player:update(dt)
  enemy:update(dt)

  ai:update(dt)
  
  updateMousePos()
  --Have the camera follow the player.
  if debug then else
  camera:lookAt(player.pos.x-grid/2, player.pos.y-grid/2)
  end
end

function game:draw()
  --Camera only affects how the game is drawn between attach() and detach()
  camera:attach()
  --Stencils only allow certain parts of the image to be drawn
  stencil = function()
    
    --Show a small circle of view around each ping
    for i = 1, #pingFade, 1 do
      if pingFade[i] ~= nil then
        --Grow
        love.graphics.circle('fill',pingFade[i].x-64,pingFade[i].y-64,64*((pingFade[i].timer)/255))
      end
    end
    --Show a circle of view around the player
    love.graphics.circle('fill',player.pos.x-grid/2,player.pos.y-grid/2,220)
  end
  --Start drawing with the stencil
  if debug then else
  love.graphics.setStencil(stencil)  end
  
  --continue with normal blending
  --love.graphics.setBlendMode("alpha")
  
  if map then
    for i = 1, #map, 1 do
      for j = 1, #map[1], 1 do
        --If the given grid position is set to an obstacle value ('1'), then draw it as such
        if map[i][j] == 1 then
          --Draw a dark, blue color for obstacles
          love.graphics.setColor(0,0,255,64)
          love.graphics.rectangle('fill', i*grid-grid, j*grid-grid, grid, grid)
        end
      end
      
    end
  end
  
  player:draw()
  enemy:draw()
  
 
  --Ends the camera drawing. If you don't call detach(), the game crashes. Seems to be that the camera supports
  --layers and so will keep drawing on top of itself. Haven't looked into this too much yet.
  camera:detach()
  
  --Draw anything that needs to use screen coordinates and not world coordinates after the camera is detached
  --Calling a blank function clears the stencil, allowing us to draw on top of it
  love.graphics.setStencil()

  DrawHUD()
  
  if enemy.dead then
    love.graphics.setColor(255,255,255,255)
    love.graphics.draw(hud.victory, screen.size.w/2 - hud.victory:getWidth()/2, screen.size.h/2 - hud.victory:getHeight()/2)
  elseif player.dead then
    love.graphics.setColor(255,255,255,255)
    love.graphics.draw(hud.defeat, screen.size.w/2 - hud.defeat:getWidth()/2, screen.size.h/2 - hud.defeat:getHeight()/2)
  end
  
  
  --Draw the crosshairs
  love.graphics.setColor(255,255,255,255)
  local mx,my = love.mouse.getPosition()
  love.graphics.draw(crosshairs, mx - crosshairs:getWidth()/2, my - crosshairs:getWidth()/2)
end
--
--
function game:keypressed(k)  
  if not player.dead then
    player:kp(k)
  end
end
function game:keyreleased(k)
  if not player.dead then
    player:kr(k)
  end
end
function game:mousepressed(x,y,b)
  if not hud.focus then
    if not player.dead then
      player:mp(x,y,b)
    end
  end
end
function game:mousereleased(x,y,b)
  if not hud.focus then
    if not player.dead then
      player:mr(x,y,b)
    end
  else
    if hud.quit.on then
      love.audio.play(menuMusic)
      Gamestate.switch(mainMenu)
    elseif hud.audio.on then
      isAudioOn = not isAudioOn
      if love.audio.getVolume() ~= 0 then
        love.audio.setVolume(0)
      else
        love.audio.setVolume(1)
      end
      
    end
    
  end
end
--
function updateMousePos()
  local x,y = love.mouse.getPosition()
  if y <= 65*screen.scale then
    hud.focus = true
  else
    hud.focus = false
  end
  
  
  if x >= screen.size.w - 10*screen.scale - hud.quit.i:getWidth() and x <= screen.size.w - 10*screen.scale and y >= 60/2*screen.scale-hud.quit.i:getHeight()/2 and y <= 60/2*screen.scale+hud.quit.i:getHeight()/2 then
    hud.quit.on = true
  else
    hud.quit.on = false
  end
  if x >= 1500*screen.scale and x <= 1500*screen.scale + hud.audio.i:getWidth() and y >= 60/2*screen.scale-hud.audio.i:getHeight()/2 and y <= 60/2*screen.scale+hud.audio.i:getHeight()/2 then
    hud.audio.on = true
  else
    hud.audio.on = false
  end
  
end
--
--
function DrawHUD()

  --Draw HUD
  love.graphics.setLineWidth(1)
  love.graphics.setColor(0,0,0,255)
  love.graphics.rectangle('fill', 0,0,5*screen.scale, screen.size.h)
  love.graphics.rectangle('fill', screen.size.w - 5*screen.scale,0,5*screen.scale, screen.size.h)
  love.graphics.rectangle('fill', 0, screen.size.h - 5*screen.scale, screen.size.w, 5*screen.scale)
  love.graphics.rectangle('fill', 0,0, screen.size.w, 65*screen.scale)
    love.graphics.setColor(0,255,255,255)
  love.graphics.rectangle('line', 5*screen.scale,60*screen.scale, screen.size.w - 10*screen.scale, screen.size.h - 65*screen.scale)
  
  --Player Health
  love.graphics.setColor(255,255,255,255)
  love.graphics.draw(hud.health, 10*screen.scale,60/2*screen.scale-hud.health:getHeight()/2)
  if player.health >= 3 then
    love.graphics.setColor(0,255,0,255)
    love.graphics.rectangle('fill', 20*screen.scale + hud.health:getWidth(), 60/2*screen.scale - 15*screen.scale, 90*screen.scale, 30*screen.scale)
  elseif player.health == 2 then
    love.graphics.setColor(255,255,0,255)
    love.graphics.rectangle('fill', 20*screen.scale + hud.health:getWidth(), 60/2*screen.scale - 15*screen.scale, 60*screen.scale, 30*screen.scale)
  elseif player.health == 1 then
    love.graphics.setColor(255,0,0,255)
    love.graphics.rectangle('fill', 20*screen.scale + hud.health:getWidth(), 60/2*screen.scale - 15*screen.scale, 30*screen.scale, 30*screen.scale)
  else
  end
  love.graphics.setColor(255,255,255,255)
  love.graphics.line(50*screen.scale + hud.health:getWidth(), 60/2*screen.scale - 15*screen.scale, 50*screen.scale + hud.health:getWidth(), 60/2*screen.scale - 15*screen.scale+30*screen.scale)
  love.graphics.line(80*screen.scale + hud.health:getWidth(), 60/2*screen.scale - 15*screen.scale, 80*screen.scale + hud.health:getWidth(), 60/2*screen.scale - 15*screen.scale+30*screen.scale)
  love.graphics.rectangle('line', 20*screen.scale + hud.health:getWidth(), 60/2*screen.scale - 15*screen.scale, 90*screen.scale, 30*screen.scale)
  
  --Enemy Health
  love.graphics.setColor(255,255,255,255)
  love.graphics.draw(hud.enemy, 500*screen.scale,60/2*screen.scale-hud.enemy:getHeight()/2)
  if enemy.health >= 3 then
    love.graphics.setColor(0,255,0,255)
    love.graphics.rectangle('fill', 510*screen.scale + hud.enemy:getWidth(), 60/2*screen.scale - 15*screen.scale, 90*screen.scale, 30*screen.scale)
  elseif enemy.health == 2 then
    love.graphics.setColor(255,255,0,255)
    love.graphics.rectangle('fill', 510*screen.scale + hud.enemy:getWidth(), 60/2*screen.scale - 15*screen.scale, 60*screen.scale, 30*screen.scale)
  elseif enemy.health == 1 then
    love.graphics.setColor(255,0,0,255)
    love.graphics.rectangle('fill', 510*screen.scale + hud.enemy:getWidth(), 60/2*screen.scale - 15*screen.scale, 30*screen.scale, 30*screen.scale)
  else
end
  love.graphics.setColor(255,255,255,255)
  love.graphics.line(540*screen.scale + hud.enemy:getWidth(), 60/2*screen.scale - 15*screen.scale, 540*screen.scale + hud.enemy:getWidth(), 60/2*screen.scale - 15*screen.scale+30*screen.scale)
  love.graphics.line(570*screen.scale + hud.enemy:getWidth(), 60/2*screen.scale - 15*screen.scale, 570*screen.scale + hud.enemy:getWidth(), 60/2*screen.scale - 15*screen.scale+30*screen.scale)
  love.graphics.rectangle('line', 510*screen.scale + hud.enemy:getWidth(), 60/2*screen.scale - 15*screen.scale, 90*screen.scale, 30*screen.scale)
  
  --Audio Toggle
  if hud.audio.on then
    love.graphics.setColor(255,0,0,255)
  elseif love.audio.getVolume() ~= 0 then
    love.graphics.setColor(0,255,0,255)
  else
    love.graphics.setColor(0,255,0,64)
  end
  love.graphics.draw(hud.audio.i, 1500*screen.scale,60/2*screen.scale-hud.enemy:getHeight()/2)
  
  --Reloading
  if player.canFire then
  else
    love.graphics.setColor(255,255,0,255)
    love.graphics.draw(hud.reload, 1000*screen.scale,60/2*screen.scale-hud.reload:getHeight()/2)
  end
  
  
  --Quit Button
  if hud.quit.on then
    love.graphics.setColor(255,0,0,255)
  else
    love.graphics.setColor(0,255,0,255)
  end
  love.graphics.draw(hud.quit.i, screen.size.w - 10*screen.scale - hud.quit.i:getWidth(), 60/2*screen.scale-hud.quit.i:getHeight()/2)
end
